import java.util.Arrays;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: HP
 * Date: 2023 -06 -10
 * Time: 21:03
 */
public class Test {
    public static void insertSort(int[] array) {
        for (int i = 0; i < array.length - 1; i++) {//排好的边界
            int cur = i;
            int val = array[i + 1];
            while (cur >= 0) {
                if (array[cur] > val) {
                    array[cur + 1] = array[cur];
                    cur--;
                } else {
                    break;
                }
            }
            array[cur+1] = val;
        }
    }

    public static void shellSort(int[] array) {
        int gap = array.length;
        while (gap > 1) {
            gap /= 2;
            for (int i = 0; i < array.length - gap; i++) {
                int cur = i;
                int val = array[cur + gap];
                while (cur >= 0) {
                    if (array[cur] > val) {
                        array[cur + gap] = array[cur];
                        cur -= gap;
                    } else {
                        break;
                    }
                }
                array[cur+gap] = val;
            }
        }
    }

    public static void selectSort(int[] array) {
        for (int i = 0; i < array.length; i++) {
            int mi = i;
            for (int j = i + 1; j < array.length; j++) {
                if (array[mi] > array[j]) {
                    mi = j;
                }
            }
            swap(array, i, mi);
        }
    }

    public static void shiftDown(int[] array, int root, int len) {
        int child = root * 2 + 1;
        while (child < len) {
            if (child + 1 < len && array[child + 1] > array[child]) {
                child += 1;
            }
            if (array[child] > array[root]) {
                swap(array, root, child);

                root = child;
                child = child * 2 + 1;
            } else {
                break;
            }
        }
    }

    //升序建大堆
    public static void heapSort(int[] array) {
        for (int i = (array.length - 1 - 1) / 2; i >= 0; i--) {
            shiftDown(array, i, array.length);
        }
        for (int i = array.length - 1; i >= 0; i--) {
            swap(array, 0, i);
            shiftDown(array, 0, i);
        }
    }

    public static void bubbleSort(int[] array) {
        for (int i = 0; i < array.length - 1; i++) {
            for (int j = 0; j < array.length - 1 - i; j++) {
                if (array[j] > array[j + 1]) {
                    swap(array, j, j + 1);
                }
            }
        }
    }

    public static void swap(int[] array, int i, int j) {
        int t = array[j];
        array[j] = array[i];
        array[i] = t;
    }


    public static void partSort(int[] array, int left, int right) {
        if (left >= right) return;
        int idx = (left + right) / 2;
        int val = Math.min(array[idx], Math.max(array[left], array[right]));//中值
        if (val != array[idx]) {//矫正中值下标
            idx = val == array[left] ? left : right;
        }
        //交换至最右
        swap(array, idx, right);

        idx = left;
        val = array[right];
        for (int i = left; i < right; i++) {
            if (array[i] < val) {
                swap(array, idx, i);
                idx++;
            }
        }
        swap(array, right, idx);//确定基准值
        partSort(array, idx + 1, right);
        partSort(array, left, idx - 1);
    }

    public static void quickSort(int[] array) {
        partSort(array, 0, array.length-1);
    }

    public static void merge(int[] array, int left, int right, int[] tmp) {
        if (left >= right) return;
        int mid = (left + right) / 2;
        merge(array, left, mid, tmp);
        merge(array, mid + 1, right, tmp);
        int s1 = left, s2 = mid + 1;
        int e1 = mid, e2 = right;
        int p = left;
        while (s1 <= e1 && s2 <= e2) {
            if (array[s1] < array[s2]) {
                tmp[p++] = array[s1++];
            } else {
                tmp[p++] = array[s2++];
            }
        }
        while (s1 <= e1) {
            tmp[p++] = array[s1++];
        }
        while (s2 <= e2) {
            tmp[p++] = array[s2++];
        }
        for (int i = left; i <= right; i++) {
            array[i] = tmp[i];
        }
    }

    public static void mergeSort(int[] array) {
        int[] tmp = new int[array.length];
        merge(array, 0, array.length-1, tmp);
    }

    public static void main(String[] args) {
        int[] arr = {9, 5, 2, 1, 3, 7, 8, 4, 5, 6, 1, 3, 6};
        insertSort(arr);
        for (int e :
                arr) {
            System.out.print(e + " ");
        }
        System.out.println();
        int[] arr1 = {9, 5, 2, 1, 3, 7, 8, 4, 5, 6, 1, 3, 6};
        shellSort(arr1);
        for (int e :
                arr1) {
            System.out.print(e + " ");
        }
        System.out.println();
        int[] arr2 = {9, 5, 2, 1, 3, 7, 8, 4, 5, 6, 1, 3, 6};
        selectSort(arr2);
        for (int e :
                arr2) {
            System.out.print(e + " ");
        }
        System.out.println();
        int[] arr3 = {9, 5, 2, 1, 3, 7, 8, 4, 5, 6, 1, 3, 6};
        heapSort(arr3);
        for (int e :
                arr3) {
            System.out.print(e + " ");
        }
        System.out.println();
        int[] arr4 = {9, 5, 2, 1, 3, 7, 8, 4, 5, 6, 1, 3, 6};
        bubbleSort(arr4);
        for (int e :
                arr4) {
            System.out.print(e + " ");
        }
        System.out.println();
        int[] arr5 = {9, 5, 2, 1, 3, 7, 8, 4, 5, 6, 1, 3, 6};
        quickSort(arr5);
        for (int e :
                arr5) {
            System.out.print(e + " ");
        }
        System.out.println();
        int[] arr6 = {9, 5, 2, 1, 3, 7, 8, 4, 5, 6, 1, 3, 6};
        mergeSort(arr6);
        for (int e :
                arr6) {
            System.out.print(e + " ");
        }
        System.out.println();
    }
}
